# 作者： 迷路的小怪兽
# 创建时间： 2024/3/24 13:56
import time
import os
import winreg
import threading
import tkinter as tk
"""
先pip安装mitmproxy
后给浏览器安装证书：在命令行执行一下mitmdump，然后到用户文件夹中的.mitmproxy文件夹找证书导入
直接mitmdump -p port -s main.py启动
设置浏览器代理即可
"""
class RecordTool:

    def __init__(self):
        self.root = tk.Tk()
        self.root.title('RecordTool')
        self.root.maxsize(300, 200)
        self.root.minsize(300, 200)


        self.IP_text_label = tk.Label(self.root, text='地址:')
        self.IP_text_label.place(x=80, y=10)

        self.IP_label = tk.Label(self.root, text='127.0.0.1')
        # self.IP_label.grid(row=1, column=2, columnspan=2)
        self.IP_label.place(x=120, y=10)

        self.PORT_text_label = tk.Label(self.root, text='端口:')
        # self.PORT_text_label.grid(row=2, column=1)
        self.PORT_text_label.place(x=80, y=50)

        self.PORT_INPUT = tk.Entry(self.root, width=10)
        # self.PORT_INPUT.grid(row=2, column=2, columnspan=2)
        self.PORT_INPUT.place(x=120, y=50)

        self.record_btn = tk.Button(self.root, text='开始录制', command=self.start)
        # self.record_btn.grid(row=3, column=1)
        self.record_btn.place(x=80, y=80)

        # bro_btn = tk.Button(self.root, text='开启浏览器', command=start_browser)
        # bro_btn.grid(row=3, column=2)

        self.stop_btn = tk.Button(self.root, text='停止录制', command=self.stop)
        # self.stop_btn.grid(row=3, column=3)
        self.stop_btn.place(x=160, y=80)

        self.root.mainloop()

    def start(self):
        self.stop()
        t1 = threading.Thread(target=self.set_proxy)
        t1.start()

    def set_proxy(self, ip='127.0.0.1', port=8090):
        if self.PORT_INPUT.get().isdigit():
            port = self.PORT_INPUT.get()
        proxy_server = f"http={ip}:{port};https={ip}:{port}"

        # 打开注册表项
        with winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows\CurrentVersion\Internet Settings', 0,
                            winreg.KEY_WRITE) as key:
            # 设置代理服务器
            winreg.SetValueEx(key, 'ProxyServer', 0, winreg.REG_SZ, proxy_server)

            # 启用代理配置
            winreg.SetValueEx(key, 'ProxyEnable', 0, winreg.REG_DWORD, 1)

            # # 勾选请勿将代理服务器用于本地(intranet)地址项
            # winreg.SetValueEx(key, 'ProxyOverride', 0, winreg.REG_SZ, ';<local>')
            # # 去掉勾选请勿将代理服务器用于本地(intranet)地址项
            # winreg.SetValueEx(key, 'ProxyOverride', 0, winreg.REG_SZ, '')


        # 开启mitmdump进程
        cmd = f'mitmdump -p {port} -s ./proxyServer.py'
        os.system(cmd)

    def stop(self):
        # 打开注册表项
        with winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows\CurrentVersion\Internet Settings', 0,
                            winreg.KEY_WRITE) as key:
            # 关闭代理配置
            winreg.SetValueEx(key, 'ProxyEnable', 0, winreg.REG_DWORD, 0)

        # 杀掉mitmdump进程
        cmd = 'TASKKILL /F /IM mitmdump.exe'
        os.system(cmd)


if __name__ == '__main__':
    RecordTool()
